import requests
import re
import time

key="python"
url="https://yq.aliyun.com/search/articles/"
data = requests.get(url,params={"q":key}).text 
pat1='<div class="_search-info">找到(.*?)条关于' 
# 多少条数据
allline=re.compile(pat1,re.S).findall(data)[0] 
allpage=int(allline) # 15+1


sor=1
for i in range(0,int(allpage)):
  print("-----正在爬取第"+str(i+1) +"页数据--------\n")
  index=str(i+1)
  parm={"q":key,"p":index}
  data=requests.get(url,params=parm).text
  pat_rul='<div class="media-body text-overflow">.*? <a href="(.*?)">'
  articles=re.compile(pat_rul,re.S).findall(data)
  for art_url in articles:
    thisurl="https://yq.aliyun.com"+art_url
    thisdata=requests.get(thisurl).text

    pat_title='<h2 class="blog-title">(.*?)</h2>'
    pat_content='<div class="content-detail unsafe markdown-body">(.*?)</div>'
    title=re.compile(pat_title,re.S).findall(thisdata)[0]
    content=re.compile(pat_content,re.S).findall(thisdata)[0]
    if sor>=100 :
      break
    fh=open("C:\\Users\\lenovo\\Desktop\\py\\yq\\"+str(i+1)+"_"+ str(sor)+".html","w",encoding="utf-8")
    fh.write(title+"<br/>---------------<br/>"+content)
    fh.close()
    sor=sor+1
  if sor>=100 :
      break
